import { patch, fetch, cascade, post } from 'plugins/axios'
import { Dialog, QSpinnerGears } from 'quasar'

export function save ({state, commit}) {
  let url = '/project/' + state.fields.id + '/'
  Dialog.create({
    title: '编辑项目',
    message: '确定修改目？',
    ok: '提交',
    cancel: '再想想'
  }).then(() => {
    state.loading.show({spinner: QSpinnerGears})
    patch(url,
      state.fields
    ).then((response) => {
      commit('saveSuccess')
    }).catch((error) => {
      if (error.response.data) {
        commit('saveFail', error.response.data)
      }
    })
  }).catch(() => {
    console.log('stay')
  })
}

export function copy ({state, commit}) {
  Dialog.create({
    title: '复制项目',
    message: '确定复制项目？',
    ok: '提交',
    cancel: '再想想'
  }).then(() => {
    state.loading.show({spinner: QSpinnerGears})
    post('/project/',
      state.fields
    ).then((response) => {
      commit('saveSuccess')
    }).catch((error) => {
      if (error.response.data) {
        commit('saveFail', error.response.data)
      }
    })
  }).catch(() => {
    console.log('stay')
  })
}

export function projectJavaopt ({state, commit}) {
  return new Promise((resolve, reject) => {
    fetch('/project/get_javaopt/', {
    }).then((response) => {
      commit('projectJavaoptSuccess', response)
      resolve()
    }).catch((error) => {
      state.etext = error.response
    })
  }).catch((error) => {
    console.log(error)
  })
}

export function projectGetPubcmd ({state, commit}) {
  return new Promise((resolve, reject) => {
    fetch('/project/get_pubcmd/', {
      env: state.fields.env
    }).then((response) => {
      commit('projectGetPubcmdSuccess', response)
      resolve()
    }).catch((error) => {
      state.etext = error.response
    })
  }).catch((error) => {
    console.log(error)
  })
}

export function projectGetHost ({state, commit}) {
  return new Promise((resolve, reject) => {
    fetch('/project/get_host/', {
      env: state.fields.env,
      id: state.fields.id
    }).then((response) => {
      commit('projectGetHostSuccess', response)
      resolve()
    }).catch((error) => {
      state.etext = error.response
    })
  }).catch((error) => {
    console.log(error)
  })
}

export function projectGetService ({state, commit}) {
  return new Promise((resolve, reject) => {
    fetch('/project/get_service/', {
      env: state.fields.env
    }).then((response) => {
      commit('projectGetServiceSuccess', response)
      resolve()
    }).catch((error) => {
      state.etext = error.response
    })
  }).catch((error) => {
    console.log(error)
  })
}

export function getDockerCmd ({state, commit}) {
  return new Promise((resolve, reject) => {
    fetch('/project/get_dockercmd/', {
      env: state.fields.env
    }).then((response) => {
      commit('getDockerCmdSuccess', response)
      resolve()
    }).catch((error) => {
      state.etext = error.response
    })
  }).catch((error) => {
    console.log(error)
  })
}

export function getDockerOpt ({state, commit}) {
  return new Promise((resolve, reject) => {
    fetch('/project/get_dockeropt/', {
    }).then((response) => {
      commit('getDockerOptSuccess', response)
      resolve()
    }).catch((error) => {
      state.etext = error.response
    })
  }).catch((error) => {
    console.log(error)
  })
}

export function loadData ({state, commit}, payload) {
  return new Promise((resolve, reject) => {
    let url = '/project/' + payload + '/'
    fetch(url, {
    }).then((response) => {
      commit('loadSucess', response)
      resolve()
    }).catch((error) => {
      alert(error.response)
    })
  }).catch((error) => {
    console.log(error)
  })
}

export function handlerPublishType ({state, commit, dispatch}, payload) {
  return new Promise((resolve, reject) => {
    let fun = []
    fun.push(commit('changePublishType', payload))
    if (state.showVMSelect && state.fields.publish_type === 1) {
      fun.push(dispatch('projectJavaopt'))
      fun.push(dispatch('projectGetPubcmd'))
    }
    Promise.all(fun).then((result) => {
      resolve()
    }).catch((error) => {
      console.log(error)
    })
  }).catch((error) => {
    console.log(error)
  })
}

export function handlerEnv ({state, commit, dispatch}, payload) {
  return new Promise((resolve, reject) => {
    let fun = []
    fun.push(commit('changeEnv', payload))
    fun.push(dispatch('projectGetHost'))
    fun.push(dispatch('projectGetService'))
    if (state.showVMSelect && state.fields.publish_type === 1 && payload) {
      fun.push(dispatch('projectGetPubcmd'))
    }
    if (state.showDockerSelect) {
      fun.push(dispatch('getDockerCmd'))
    }
    Promise.all(fun).then((result) => {
      resolve()
    }).catch((error) => {
      console.log(error)
    })
  }).catch((error) => {
    console.log(error)
  })
}

export function handlerPubVersion ({state, commit, dispatch}, payload) {
  return new Promise((resolve, reject) => {
    let fun = []
    fun.push(commit('changePubVersion', payload))
    if (state.fields.pub_version === 2) {
      fun.push(dispatch('getDockerOpt'))
      if (state.fields.env) {
        fun.push(dispatch('getDockerCmd'))
      }
    }
    Promise.all(fun).then((result) => {
      resolve()
    }).catch((error) => {
      console.log(error)
    })
  }).catch((error) => {
    console.log(error)
  })
}

export function deleteData ({state}) {
  Dialog.create({
    title: '删除主机',
    message: '确定要删库？',
    ok: '跑路',
    cancel: '再想想'
  }).then(() => {
    state.loading.show({spinner: QSpinnerGears})
    let url = '/project/' + state.fields.id + '/'
    cascade(url).then((response) => {
      state.loading.hide()
      this.$router.push('/listproject')
    }).catch((error) => {
      state.loading.hide()
      console.log(error.response.data.detail)
    })
  }).catch(() => {
    console.log('stay')
  })
}

export function loadProjectParent ({state, commit}, payload) {
  return new Promise((resolve, reject) => {
    let url = '/project/project_parent/'
    fetch(url, {
      opt: payload
    }).then((response) => {
      commit('loadProjectParentSuccess', response)
      resolve()
    }).catch((error) => {
      console.log(error.response.data.detail)
    })
  }).catch((error) => {
    console.log(error)
  })
}
